﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPCPlatformConfigure
{
    public class OracleHelper
    {
        private string connectionString;
        public OracleHelper(string connectionString)
        {
            this.connectionString = connectionString;
        }
        public DataTable GetTable(string sqlString)
        {
            OracleConnection con = new OracleConnection(connectionString);
            OracleCommand com = new OracleCommand(sqlString, con);
            DataTable dtTemp = new DataTable();
            OracleDataAdapter oda = new OracleDataAdapter(com);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
                oda.Fill(dtTemp);
                return dtTemp;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public void InsertDataMany(string sqlString, List<OracleParameterWrapper> datas)
        {
            OracleConnection con = new OracleConnection(connectionString);
            OracleCommand com = new OracleCommand(sqlString, con);
            com.ArrayBindCount = datas.First().ParameterValues.Count;
            foreach (var item in datas)
            {
                OracleParameter para = new OracleParameter(item.ParameterName, item.ParameterType);
                para.Direction = ParameterDirection.Input;
                para.Value = item.ParameterValues.ToArray();
                com.Parameters.Add(para);
            }
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
        public void ExecuteNonQuery(string sqlString)
        {
            OracleConnection con = new OracleConnection(connectionString);
            OracleCommand com = new OracleCommand(sqlString, con);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
        public class OracleParameterWrapper
        {
            public string ParameterName { get; }
            public OracleDbType ParameterType { get; }
            public List<object> ParameterValues { get; }
            public OracleParameterWrapper(string parameterName, OracleDbType parameterType)
            {
                ParameterName = parameterName;
                ParameterType = parameterType;
                ParameterValues = new List<object>();
            }
        }
    }
}
